package com.atguigu.thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadOrderTest {
    public static MyChace myChace = new MyChace();
    public static void main(String[] args) {
        for (int i = 0; i <= 3; i++) {
            new Thread(()->{
                try { myChace.round5(); } catch (InterruptedException e) { e.printStackTrace(); }
            },String.valueOf(i)).start();
            new Thread(()->{
                try { myChace.round10(); } catch (InterruptedException e) { e.printStackTrace(); }
            },String.valueOf(i)).start();
            new Thread(()->{
                try { myChace.round15(); } catch (InterruptedException e) { e.printStackTrace(); }
            },String.valueOf(i)).start();
        }
    }
//    public static void main(String[] args)
//    {
//        MyChace myChace = new MyChace();
//
//        new Thread(() -> {
//            for (int i = 1; i <=10; i++) {
//                try {
//                    myChace.round5();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        },"A").start();
//
//        new Thread(() -> {
//            for (int i = 1; i <=10; i++) {
//                try {
//                    myChace.round10();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        },"B").start();
//
//        new Thread(() -> {
//            for (int i = 1; i <=10; i++) {
//                try {
//                    myChace.round15();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        },"C").start();
//    }
}

class MyChace {
    Lock lock = new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();
    int temp = 1;

    public void round5() throws InterruptedException {
        lock.lock();
        while (temp != 1) {
            condition1.await();
        }
        try {
            for (int i = 1; i <= 5; i++) {
                int finalI = i;
                System.out.println(Thread.currentThread().getName() +"     " +finalI);
                temp = 2;
                condition2.signal();
            }
        } finally {
            lock.unlock();
        }
    }

    public void round10() throws InterruptedException {
        lock.lock();
        while (temp != 2) {
            condition1.await();
        }
        try {
            for (int i = 1; i <= 5; i++) {
                int finalI = i;
                System.out.println(Thread.currentThread().getName() +"     " + finalI);
                temp = 3;
                condition3.signal();
            }
        } finally {

            lock.unlock();
        }
    }

    public void round15() throws InterruptedException {
        lock.lock();

        try {
            while (temp != 3) {
                condition3.await();
            }
            for (int i = 1; i <= 15; i++) {
                int finalI = i;
                System.out.println(Thread.currentThread().getName() +"     " + finalI);
                temp = 1;
                condition1.signal();
            }
        } finally {

            lock.unlock();
        }
    }
}